import { useLazyFetch } from 'nuxt/app'
import { useUserManagerStore } from '~/stores/userManagerStore'

type useFetchType = typeof useLazyFetch

export const useAPILazyFetch: useFetchType = (path, options = {}) => {
  const config = useRuntimeConfig()
  const userManagerStore = useUserManagerStore()
  options.baseURL = config.public.baseURL
  options.headers = [
    ['Authorization', 'Bearer ' + (userManagerStore.user?.access_token || '')]
  ]
  return useLazyFetch(path, options)
}
